package com.yjz.study.viewmodel

import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.alibaba.android.arouter.BuildConfig
import com.yjz.common.restful.CallBack
import com.yjz.common.restful.HttpBaseResponse
import com.yjz.study.http.ApiFactory
import com.yjz.study.http.api.GoodsApi
import com.yjz.study.http.entity.res.Favorite
import com.yjz.study.http.entity.res.GoodsDetailModel
import java.lang.Exception

/**
 *作用 : 商品详情viewModel
 *作者 : yjz
 *时间 : 2025/5/14 11:52
 */
class GoodsDetailViewModel(val goodsId: String?) : ViewModel() {

    companion object {
        /**
         * 自定义viewModel的Factory，用于在创建viewModel的时候在构造器传入参数
         */
        class GoodsDetailViewModelFactory(private val goodsId: String?) : ViewModelProvider.NewInstanceFactory() {
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                try {
                    val constructor = modelClass.getConstructor(String::class.java)
                    if (constructor != null) {
                        return constructor.newInstance(goodsId)
                    }
                } catch (exception: Exception) {
                    //ignore
                }
                return super.create(modelClass)
            }
        }

        fun get(goodsId: String?, owner: ViewModelStoreOwner): GoodsDetailViewModel {
            return ViewModelProvider(owner, GoodsDetailViewModelFactory(goodsId))[GoodsDetailViewModel::class.java]
        }
    }

    /**
     * 获取商品详情
     */
    fun queryDetailData(): LiveData<GoodsDetailModel> {
        val liveData = MutableLiveData<GoodsDetailModel>()
        if (!TextUtils.isEmpty(goodsId)) {
            ApiFactory.create(GoodsApi::class.java)
                .queryDetail(goodsId!!).enqueue(object : CallBack<GoodsDetailModel> {
                    override fun onSuccess(response: HttpBaseResponse<GoodsDetailModel>) {
                        if (response.successful() && response.data != null) {
                            liveData.postValue(response.data)
                        } else {
                            liveData.postValue(null)
                        }
                    }

                    override fun onFailed(throwable: Throwable) {
                        liveData.postValue(null)
                        if (BuildConfig.DEBUG) {
                            throwable.printStackTrace()
                        }
                    }
                })
        }
        return liveData
    }

    /**
     * 收藏和取消收藏
     */
    fun toggleFavorite(): LiveData<Boolean?> {
        val toggleFavoriteData = MutableLiveData<Boolean?>()
        if (!TextUtils.isEmpty(goodsId)) {
            ApiFactory.create(GoodsApi::class.java).favorite(goodsId!!)
                .enqueue(object : CallBack<Favorite> {
                    override fun onSuccess(response: HttpBaseResponse<Favorite>) {
                        toggleFavoriteData.postValue(response.data?.isFavorite)
                    }

                    override fun onFailed(throwable: Throwable) {
                        toggleFavoriteData.postValue(null)
                    }
                })
        }
        return toggleFavoriteData
    }
}